#include <vector>
#include <algorithm>
using namespace std;
/*
 * @lc app=leetcode.cn id=122 lang=cpp
 *
 * [122] 买卖股票的最佳时机 II
 */

// @lc code=start
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0, max_val = INT_MAX, l = 0;
        prices.push_back(0);
        for (int i = 0; i < prices.size(); i++) {
            if (max_val == INT_MAX) max_val = prices[i];
            if (prices[i] < max_val) {
                res += max_val - prices[l];
                l = i; max_val = prices[l];
            } else max_val = prices[i];
        }
        return res;
    }
};
// @lc code=end

